﻿@page "/admin/post/draft"
@using Moonglade.Core.PostFeature
@using Moonglade.Data.Specifications
@inject IMediator Mediator
@{
    ViewBag.Title = "Drafts";
    var posts = await Mediator.Send(new ListPostSegmentByStatusQuery(PostStatus.Draft));
}

@section scripts{
    <script type="module">
        import * as utils from '/js/app/utils.module.js'
        
        window.deletePost = function(postid) {
            callApi(`/api/post/${postid}/recycle`, 'DELETE', {}, function(resp) {
                document.querySelector(`#post-${postid}`).style.display = 'none';
            });
        }

        document.querySelectorAll(".btn-delete").forEach(function (button) {
            button.addEventListener("click", function () {
                var cfm = confirm("Delete Confirmation?");
                if (cfm) {
                    window.deletePost(this.dataset.postid);
                }
            });
        });
        
        utils.formatUtcTime();
    </script>
}

@section admintoolbar{
    <partial name="_PostManageHeader" />
}

<div>
    @if (posts.Any())
    {
        @Html.AntiForgeryToken()

        @foreach (var post in posts.OrderByDescending(p => p.LastModifiedUtc))
        {
            <div id="post-@post.Id" class="admin-post-entry p-3 mb-2 rounded-3 shadow-sm border">
                <div class="d-flex">
                    <div class="flex-grow-1">
                        <a asp-page="./EditPost" asp-route-id="@post.Id">
                            <h6>
                                @post.Title
                            </h6>
                        </a>
                        <div>
                            <i class="bi-calendar-event"></i>
                            <span class="text-muted me-2">
                                <time data-utc-label="@post.CreateTimeUtc.ToString("u")">@post.CreateTimeUtc</time>
                            </span>
                        </div>
                    </div>

                    <div>
                        <a class="btn btn-outline-accent btn-sm btn-edit" asp-page="./EditPost" asp-route-id="@post.Id"><i class="bi-pen"></i></a>
                        <a class="btn btn-outline-danger btn-sm btn-delete" data-postid="@post.Id" href="javascript:;">
                            <i class="bi-trash"></i>
                        </a>
                    </div>
                </div>
            </div>
        }
    }
    else
    {
        <partial name="_NoPosts" />
    }
</div>